// 46.全排列
package LeetcodeHot100;

import java.util.ArrayList;
import java.util.List;

public class Solution046 {
    List<List<Integer>> result = new ArrayList<>();

    public List<List<Integer>> permute(int[] nums) {
        boolean[] isVisit = new boolean[nums.length];

        backTrace(nums, new ArrayList<Integer>(), isVisit, 0);

        return result;
    }

    private void backTrace(int[] nums, List<Integer> list, boolean[] isVisit, int cur) {
        if (cur == nums.length)
            result.add(new ArrayList<>(list));
        for (int i = 0; i < nums.length; i++) {
            if (isVisit[i] == false) {
                list.add(nums[i]);
                isVisit[i] = true;
                backTrace(nums, list, isVisit, cur + 1);
                isVisit[i] = false;
                list.remove(list.size() - 1);
            }
        }
    }
}
